//
//  HSYBaseMoveNavigationController+Animation.m
//  高仿暴走斗图
//
//  Created by key on 16/4/22.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "HSYBaseMoveNavigationController+Animation.h"
#import "UINavigationController+Manager.h"

@implementation HSYBaseMoveNavigationController (Animation)

- (void)customMovePushViewController:(UIViewController *)viewController animated:(BOOL)animated completion:(void (^)(BOOL isAnimation))completion {
    
    if (animated) {
        
        UIImageView *bottom = [self getScreenImageViewForFrame:(CGRect){0, 0, self.view.viewSize} andImage:[self.screenViewControllers objectAtIndex:self.viewControllers.count - 1]];//[self getScreenImageViewForFrame:(CGRect){0, 0, self.view.viewSize} captureImageInView:self.view];
        
        [self pushViewController:viewController animated:NO];
        
        UIImageView *top = [self getScreenImageViewForFrame:(CGRect){self.view.viewWidth, 0, self.view.viewSize} andImage:[self.screenViewControllers lastObject]];//[self getScreenImageViewForFrame:(CGRect){self.view.viewWidth, 0, self.view.viewSize} captureImageInView:self.view];
        
        [[UIScreen getKeyWindows] addSubview:top];
        [self.view insertSubview:bottom belowSubview:[UIScreen getKeyWindows]];
        
        @weakify(self);
        [UIView animateWithDuration:[self animationDuration] animations:^{
            
            @strongify(self);
            bottom.frame = (CGRect){-self.view.viewWidth/3, 0, self.view.viewSize};
            top.frame = (CGRect){0, 0, self.view.viewSize};
            
        } completion:^(BOOL finished) {
            
            [bottom removeFromSuperview];
            [top removeFromSuperview];
            completion(finished);
        }];
        
    } else {
        
        [self pushViewController:viewController animated:NO];
        completion(YES);
    }
}


- (void)customMovePOPViewControllerForAnimated:(BOOL)animated completion:(void (^)(BOOL isAnimation))completion {
    
    if (animated) {
        
        UIImageView *top = [self getScreenImageViewForFrame:(CGRect){0, 0, self.view.viewSize} andImage:[self.screenViewControllers lastObject]];//[self getScreenImageViewForFrame:(CGRect){0, 0, self.view.viewSize} captureImageInView:self.view];
        
        UIImageView *bottom = [self getScreenImageViewForFrame:(CGRect){-self.view.viewWidth/3, 0, self.view.viewSize} andImage:[self.screenViewControllers objectAtIndex:self.viewControllers.count - 2]];
        
        [[UIScreen getKeyWindows] addSubview:top];
        [self.view insertSubview:bottom belowSubview:[UIScreen getKeyWindows]];
        
        @weakify(self);
        [UIView animateWithDuration:[self animationDuration] animations:^{
            
            @strongify(self);
            top.frame = (CGRect){self.view.viewWidth, 0, self.view.viewSize};
            bottom.frame = (CGRect){0, 0, self.view.viewSize};
            
        } completion:^(BOOL finished) {
            
            [top removeFromSuperview];
            [bottom removeFromSuperview];
            [self popViewControllerAnimated:NO];
            completion(finished);
        }];
        
    } else {
        
        [self popViewControllerAnimated:NO];
        completion(YES);
    }
}

@end
